<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller {

	public function __construct()
    {
        parent::__construct();
		$this->load->helper('cookie');
		$this->load->library('Session');
		$this->load->model('Manager_model','manager_model');
		$this->load->library('form_validation');
		$this->config->load('blog_settings');
    }

	//登录页
	public function index(){
		$userid = $this->session->userid;
		if($userid){
			redirect('admin');
		}
		$data['cap'] = $this->getcap();
		$this->load->view('admin/login.html',$data);
	}
	
	//获取验证码
	private function getcap(){
		$this->load->helper('captcha');
		$vals = array(
			'img_path' => $this->config->item('captchapath'),
			'img_url' => base_url().$this->config->item('captchapath'),
			'word_length'   => 4,
			'expiration'    => 60,
			'img_width' => '80'
			);
		$data['cap'] = create_captcha($vals);
		$this->session->capword = $data['cap']['word'];
		return $data['cap'];
	}

	public function getcapimg(){
		$img = $this->getcap();
		echo $img['image'];
	}

	//登录表单验证
	public function cklogin(){
		$cpa = $this->input->post('cpa');
		$capword = $this->session->capword;
		if(!$cpa  or  $cpa != $capword){
				$data['alert'] ="验证码错误！";
				$data['cap'] = $this->getcap();
				$this->load->view('admin/login.html',$data);
				return false;
		}
		$name = $this->input->post('name');
		$password = md5($this->input->post('password'));
		$res = $this->manager_model->getuserbyname($name);
		if($res){
			if($password != $res->password){
					$data['alert'] ="账号或密码错误！";
					$data['cap'] = $this->getcap();
					$this->load->view('admin/login.html',$data);
					return false;
			}else{
				$this->session->name=$name;
				$this->session->userid=$res->id;
				$this->session->userlev=$res->level;
				$cookie = array(
					'name'   => 'name',
					'value'  => $res->name,
					'expire' => '80000'
				);
				$this->input->set_cookie($cookie);
				$cookie2 = array(
					'name'   => 'userid',
					'value'  => $res->id,
					'expire' => '80000'
				);
				$this->input->set_cookie($cookie2);
				//userpic
				$userpic=$res->userpic;
				if($userpic and $userpic!=''){
					$reg = '/((http|https):\/\/)+(\w+\.)+(\w+)[\w\/\.\-]*(jpg|png)/';
					preg_match($reg,$userpic,$result);
					if(!$result){
						$userpic = base_url().$this->config->item('userpicpath').$userpic;
					}
				}else{
					$userpic = base_url('resource/adminlte/dist/img/avatar5.png');
				}
				$cookie3 = array(
					'name'   => 'userimg',
					'value'  => $userpic,
					'expire' => '80000'
				);
				$this->input->set_cookie($cookie3);
				$this->manager_model->replacelogtime($res->id);
				redirect('admin');
			}
		}else{
			$data['alert'] ="账号或密码错误！";
			$data['cap'] = $this->getcap();
			$this->load->view('admin/login.html',$data);
			return false;
		}

	}

	//退出登录
	public function loginout(){
		$this->input->set_cookie('username','');
		$this->input->set_cookie('userid','');
		$this->session->unset_userdata('name');
		$this->session->unset_userdata('userid');
		redirect('login');
	}












}